package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbSpecAndOption;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specificationController")
public class SpecificationController {
    @Reference
    private SpecificationService specificationService;
    @Reference
    private SpecificationOptionService specificationOptionService;


    //跳转规格管理
    @RequestMapping("toSpecification")
    public String toSpecification(){
        return "admin/specification";
    }

    //查询所有规格管理信息
    @RequestMapping("selectspec")
    @ResponseBody
    public StatusCode selectSpecification(String specName){
        List<TbSpecification> list = specificationService.findAll(specName);
        if (list == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(list);
    }


    //规格增加
    @RequestMapping("insertspec")
    @ResponseBody
    public StatusCode insertSpec(TbSpecification tbSpecification,String optionListJson){
        if (tbSpecification == null && optionListJson == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        //将字符串转换为集合
        List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        Boolean b = specificationOptionService.insertSpec(tbSpecification,tbSpecificationOptions);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //回显
    @RequestMapping("updatespec")
    @ResponseBody
    public StatusCode updateSpec(Long id){
        if (id == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        //查询规格名称
        TbSpecification tbSpecification = specificationService.selectSpecById(id);
        //查询规格选项
        List<TbSpecificationOption> tbSpecificationOptions = specificationOptionService.selectOptionBySpecid(id);
        if (tbSpecification == null && tbSpecificationOptions == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        TbSpecAndOption tbSpecAndOption = new TbSpecAndOption(tbSpecification,tbSpecificationOptions);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbSpecAndOption);
    }

    //修改
    @RequestMapping("updatespec2")
    @ResponseBody
    public StatusCode updateSpec2(TbSpecification tbSpecification,String optionListJson){
        if (tbSpecification == null && optionListJson == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        //将字符串转换为集合
        List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        //修改
        Boolean b = specificationOptionService.updateSpec(tbSpecification,tbSpecificationOptions);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //删除
    @RequestMapping("deleteById")
    @ResponseBody
    public StatusCode deleteById(Long[] ids){
        if (ids == null && "".equals(ids)){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        //删除规格
        Boolean b = specificationService.deleteById(ids);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }
}
